#include<iostream>
#include<unordered_set>

using namespace std;



bool isHappy(int n) {
    unordered_set<int> res;
    while(1)
    {
        int sum=0;
        while (n>0)
        {
            sum+=((n%10)*(n%10));
            n/=10;
        }
        if(sum==1)
            return true;
        if(res.find(sum)!=res.end())
        {
            return false;
        }
        else
        {
            res.insert(sum);
            n=sum;
        }

    }
}

class solution
{
    int SumSquare(int n)
    {
        int sum=0;
        while(n>0)
        {
            sum+=((n%10)*(n%10));
            n/=10;
        }
        return sum;
    }
    bool isHappy(int n)
    {
        int slow=n,fast=n;
        do{
            slow = SumSquare(slow);
            fast = SumSquare(fast);
            fast = SumSquare(fast);
        }while(slow != fast);
        
        return slow == 1;

    }
};

int main()
{
    cout<<isHappy(19);
}